package com.welsy.iter.command
{
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.welsy.iter.business.AddIterDelegate;
	import com.welsy.iter.event.AddIterEvent;
	import com.welsy.iter.model.IterModelLocator;
	import com.welsy.iter.vo.IterVO;
	
	import mx.controls.Alert;
	import mx.rpc.IResponder;
	import mx.rpc.events.FaultEvent;	
	
	/**
	  * The command for course retrieval
	  * @author Welsy.org
 	  * @version	$Revision: 1.1 $
	  */
	public class AddIterCommand implements ICommand, IResponder
	{
	  	public function AddIterCommand()
		{	 
		}
	
		/**
		  * Set up the command, starting the chain with the delegate call
		  * @param event The event which triggers the call chain
		  */
		public function execute( event : CairngormEvent ): void
		{
			var addIterEvent:AddIterEvent = AddIterEvent( event );
			
			var delegate : AddIterDelegate = new AddIterDelegate( this );
			delegate.AddIter(addIterEvent.iter);
		}
	
		/**
		  * Evaluate the SOAP result
		  * @param event The SOAP server result
		  */
		public function result( event : Object ) : void
		{			
			//set courseId for the selected treeNode
			var model : IterModelLocator = IterModelLocator.getInstance();
			var iterNode : IterVO = model.selectedIterTreeNode;
			iterNode.id = event.result.IterID;
			iterNode.isValid = true;	//mark iter as valid (will be added to the treeView)
			model.selectedIterTreeNode = null;		
			model.selectedIterTreeNode = iterNode;
		}
	
		/**
		  * The fault handler
		  * Report a simple error message to the user
		  * @param The failure event
		  */
		public function fault( event : Object ) : void
		{
			var faultEvent : FaultEvent = FaultEvent( event );
			Alert.show( "Iter could not be added!" );
		}
	}

}